<?php
/**
 * @Author: feidao@ibantang.com, 2019, all rights reserved
 * @Date: 2019-11-07
 * @Time: 17:00
 * @Brief:
 */

namespace Core\Common\Util;

class DbError
{
    private static $errorMsg = [
        'server has gone away',
        'no connection to the server',
        'Lost connection',
        'is dead or not enabled',
        'Error while sending',
        'decryption failed or bad record mac',
        'server closed the connection unexpectedly',
        'SSL connection has been closed unexpectedly',
        'Error writing data to the connection',
        'Resource deadlock avoided',
        'Transaction() on null',
        'child connection forced to terminate due to client_idle_limit',
        'query_wait_timeout',
        'reset by peer',
        'Physical connection is not usable',
        'TCP Provider: Error code 0x68',
        'Name or service not known',
        'PDO::beginTransaction(): Error reading result set\'s header',
        'Connection timed out',
        'Broken pipe',
        'Connection refused',
        "Error reading result set's header",
    ];

    /**
     * 识别是否是mysql连接错误报的错
     * @param $errMsg string 报错信息
     * @return bool
     */
    public static function causedByLostConnection($errMsg)
    {
        foreach (self::$errorMsg as $needle) {
            if ($needle !== '' && mb_strpos($errMsg, $needle) !== false) {
                return true;
            }
        }
        return false;
    }
}
